const api = require("../../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    title:app.globalData.title,
    deviceSn:null,
    devInfo:{},
    modalName:'',
    agreement:{
      title:'租赁服务协议',
      code:'RentAgreement',
      checked:true
    }
  },
  _loadAgreementData:function(){
    const _self = this;
    my.showLoading();
    api.post(api.url.agreementInfo,{
      appId:my.getAppIdSync().appId,
      code:this.data.agreement.code
    },res=>{
      console.info(res);
      my.hideLoading();
      if(res.data.code==200){
        let agreement = _self.data.agreement;
        agreement.title = res.data.body.title;
        agreement.content = res.data.body.content;
        this.setData({
          "modalName":"agreement",
          "agreement":agreement
        })
      }else if(res.data.msg){
          my.showModal({
            content: res.data.msg,
            showCancel:false
          })
      }else{
          my.showToast({
            content: '获取协议失败',
            type:'error'
          })
      }
  })
  },
  _loadDeviceData:function(){
    api.post(api.url.pclScan,{
        deviceSn:this.data.deviceSn
    },res=>{
        console.info(res);
        if(res.data.code==200){
            if(res.data.body.tradeNo){
                my.redirectTo({
                    url: '../result/result?tradeNo='+res.data.body.tradeNo,
                })
            }else{
                if(res.data.body.templateId){
                    this.setData({
                        devInfo:res.data.body
                    })
                }else{
                    my.showModal({
                        content: '未配置套餐，请联系客服',
                        showCancel:false,
                        success(){
                            my.reLaunch({
                                url: '/pages/index/index',
                            })
                        }
                    })
                }
            }
        }else if(res.data.msg){
            my.showModal({
              content: res.data.msg,
              showCancel:false,
              success(){
                  my.reLaunch({
                    url: '/pages/index/index',
                  })
              }
            })
        }else{
            my.showToast({
              content: '获取信息失败',
              type:'error'
            })
            setTimeout(function(){
                my.reLaunch({
                  url: '/pages/index/index',
                })
            },1000);
        }
    })
  },

  bindHideModal:function(e){
    this.setData({
      modalName:''
    })
  },

  btnPay:function(e){
    let index = e.currentTarget.dataset.index;
    let setMealItem = this.data.devInfo.freeItems[index];

    if(!this.data.agreement.checked){
        my.showModal({
          title: '温馨提示',
          content: '请先勾选同意授权书',
          showCancel:false
        })
        return
    }
    if(!setMealItem){
        my.showModal({
          title: '温馨提示',
          content: '请选择一个套餐使用',
          showCancel:false,
        })
        return
    }
    const self = this;
    api.post(api.url.pclPay,{
        payType:2,
        appId:my.getAppIdSync().appId,
        deviceSn:this.data.deviceSn,
        templateId:this.data.devInfo.templateId,
        setMealId:setMealItem.id
    },(res)=>{
        console.log(res)
        if(res.data.code==200){
            const tradeNo = res.data.body.tradeNo;
            my.tradePay ({
              tradeNO: tradeNo,
              success: res => {
                console.error('调用 my.tradePay 成功: ', JSON.stringify(res));
                self.toUnderway(tradeNo);
              },
              fail: error => {
                console.error('调用 my.tradePay 失败: ', JSON.stringify(error));
                my.showToast({
                  content: '支付失败',
                  type:'error'
                })
              },
            });
        }else if(res.data.msg){
            my.showModal({
              content: res.data.msg,
              showCancel:false
            })
        }else{
            my.showToast({
              content: '支付失败',
              type:'error'
            })
        }
    })
  },

  btnAgreementShow(){
    if(this.data.agreement.content){
      this.setData({
        "modalName":"agreement"
      })
    }else{
      this._loadAgreementData();
    }
  },

  toUnderway(tradeNo){
    my.redirectTo({
      url: '/packageA/pages/pcl/result/result?tradeNo='+tradeNo,
    })
  },

  onLoad(options) {
    if(options.deviceSn){
      this.data.deviceSn = options.deviceSn;
      this._loadDeviceData();
  }
  },
});
